//
//  RBAccountManager.m
//  Culture
//
//  Created by Lan on 2017/9/9.
//  Copyright © 2017年 SummerTea. All rights reserved.
//

#import "RBAccountManager.h"
#import "SAMKeychain.h"
#import "NSString+YYAdd.h"
#import "RBPersistence.h"
#import "RBConst.h"

static NSString *const RB_SPA_STR = @".c.b.a.1.TY_2.0.";
static NSString *const RB_AUX_STR = @"3s3s.10lth";

//service和account共同决定一个key
static NSString *const RB_ACCOUNT_KEY = @"RB_ACCOUNT_KEY";
static NSString *const RB_ACCOUNT_SERVICE_KEY = @"RB_ACCOUNT_SERVICE_KEY";
static NSString *const RB_PASSWORD_SERVICE_KEY = @"RB_PASSWORD_SERVICE_KEY";
static NSString *const RB_TOKEN_SERVICE_KEY = @"RB_TOKEN_SERVICE_KEY";
static NSString *const RB_UDID_SERVICE_KEY = @"RB_UDID_SERVICE_KEY";

@interface RBAccountManager ()

@end

@implementation RBAccountManager

#pragma mark - keychain存储

+ (NSString *)keyWithText: (NSString *)text
{
    return [[[text stringByAppendingString:RB_SPA_STR] hmacMD5StringWithKey:RB_AUX_STR] md2String];
}

+ (NSString *)accountKey
{
    return [self keyWithText:RB_ACCOUNT_KEY];
}

+ (NSString *)accountServiceKey
{
    return [self keyWithText:RB_ACCOUNT_SERVICE_KEY];
}

+ (NSString *)passwordServiceKey
{
    return [self keyWithText:RB_PASSWORD_SERVICE_KEY];
}

+ (NSString *)tokenServiceKey
{
    return [self keyWithText:RB_TOKEN_SERVICE_KEY];
}

+ (NSString *)getKeychainAccount
{
    return [SAMKeychain passwordForService:[self accountServiceKey] account:[self accountKey]];
}

+ (NSString *)UDIDServiceKey
{
    return [self keyWithText:RB_UDID_SERVICE_KEY];
}

+ (void)setKeychainAccount:(NSString *)account
{
    if (account.length)
    {
        [SAMKeychain setPassword:account forService:[self accountServiceKey] account:[self accountKey]];
    }
    else
    {
        [SAMKeychain deletePasswordForService:[self accountServiceKey] account:[self accountKey]];
    }
}

+ (NSString *)getKeychainPassword
{
    NSString *account = [self getKeychainAccount];
    if (account.length)
    {
        return [SAMKeychain passwordForService:[self passwordServiceKey] account:account];
    }
    else
    {
        return nil;
    }
}

+ (void)setKeychainPassword:(NSString *)password
{
    NSString *account = [self getKeychainAccount];
    if (password.length && account.length)
    {
        [SAMKeychain setPassword:password forService:[self passwordServiceKey] account:account];
    }
    else if(account.length)
    {
        [SAMKeychain deletePasswordForService:[self passwordServiceKey] account:account];
    }
}

+ (NSString *)getKeychainToken
{
    NSString *account = [self getKeychainAccount];
    if (account.length)
    {
        return [SAMKeychain passwordForService:[self tokenServiceKey] account:account];
    }
    else
    {
        return nil;
    }
}

+ (void)setKeychainToken:(NSString *)token
{
    NSString *account = [self getKeychainAccount];
    if (token.length && account.length)
    {
        [SAMKeychain setPassword:token forService:[self tokenServiceKey] account:account];
    }
    else if(account.length)
    {
        [SAMKeychain deletePasswordForService:[self tokenServiceKey] account:account];
    }
}

+ (NSString *)udidAccount
{
    return @"RB_UDID_ACCOUNT";
}

+ (NSString *)getUDID
{
    NSString *account = [self udidAccount];
    if (account.length)
    {
        NSString *udid = [SAMKeychain passwordForService:[self UDIDServiceKey] account:account];
        if (udid.length)
        {
            return udid;
        }
        else
        {
            udid = [NSString stringWithFormat:@"iOS%@%@%@%@%@%@%@", UIDevice.currentDevice.name, UIDevice.currentDevice.model, UIDevice.currentDevice.localizedModel, UIDevice.currentDevice.systemName, UIDevice.currentDevice.systemVersion, NSDate.date, UIDevice.currentDevice.identifierForVendor.UUIDString];
            [self setKeychainUDID:udid];
        }
        return udid;
    }
    else
    {
        return nil;
    }
}

+ (void)setKeychainUDID: (NSString *)UDID
{
    NSString *account = [self udidAccount];
    if (UDID.length && account.length)
    {
        [SAMKeychain setPassword:UDID forService:[self UDIDServiceKey] account:account];
    }
    else if(account.length)
    {
        [SAMKeychain deletePasswordForService:[self UDIDServiceKey] account:account];
    }
}

#pragma mark - 内存存储

+ (instancetype)manager
{
    static id instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [self new];
    });
    return instance;
}
#pragma mark - 持久化存储

static NSString *const RB_UID_KEY = @"RB_UID_KEY";

+ (NSString *)getUserId
{
    return [RBPersistence getValueWithKey:[self keyWithText:RB_UID_KEY]];
}

+ (void)setUserId: (NSString *)userId
{
    [RBPersistence storeValue:userId withKey:[self keyWithText:RB_UID_KEY]];
}

static NSString *const RB_SESSIONID_KEY = @"RB_SESSIONID_KEY";

+ (NSString *)getSessionId
{
    return [RBPersistence getValueWithKey:[self keyWithText:RB_SESSIONID_KEY]];
}

+ (void)setSessionId: (NSString *)sessionId
{
    [RBPersistence storeValue:sessionId withKey:[self keyWithText:RB_SESSIONID_KEY]];
}

static NSString *const RB_TOKEN_KEY = @"RB_TOKEN_KEY";

+ (NSString *)getToken
{
    return [RBPersistence getValueWithKey:[self keyWithText:RB_TOKEN_KEY]];
}

+ (void)setToken: (NSString *)token
{
    [RBPersistence storeValue:token withKey:[self keyWithText:RB_TOKEN_KEY]];
}

static NSString *const RB_TTC_KEY = @"RB_TTC_KEY";

+ (BOOL)ttc
{
    return [[NSUserDefaults standardUserDefaults] boolForKey:RB_TTC_KEY];
}

+ (void)setTtc: (BOOL)ttc;
{
    [[NSUserDefaults standardUserDefaults] setBool:ttc forKey:RB_TTC_KEY];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

static NSString *const RB_NET_PASS_KEY = @"RB_NET_PASS_KEY";

+ (BOOL)netPass
{
    return [[NSUserDefaults standardUserDefaults] boolForKey:RB_NET_PASS_KEY];
}

+ (void)setNetPass: (BOOL)netPass
{
    [[NSUserDefaults standardUserDefaults] setBool:netPass forKey:RB_NET_PASS_KEY];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

@end
